/**
 * 
 */
package it.fuesi.signit.fragments;

import it.fuesi.signit.CurrentSettings;
import it.fuesi.signit.R;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;

/**
 * @author pascotto
 *
 */
public class SettingsDetailFragment extends Fragment {

	private EditText mSignature;
	private RadioGroup mSize;
	private Spinner mPosition;
	private SeekBar mMargin;
	private SeekBar mStroke;
	
	private SeekBar mColorR;
	private SeekBar mColorG;
	private SeekBar mColorB;
	private ImageView mColor;
	
	private Spinner mFont;
	private Spinner mFontStyle;
	
	private CurrentSettings settings;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		super.onCreateView(inflater, container, savedInstanceState);
		
		View root =  inflater.inflate(R.layout.signature_conf, container, false);
	
		mSignature = (EditText)root.findViewById(R.id.signature);
		mSize = (RadioGroup)root.findViewById(R.id.size);
		
		mPosition = (Spinner)root.findViewById(R.id.position);
		final String[] positions = getResources().getStringArray(R.array.settings_positions);
		final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, positions);
		mPosition.setAdapter(adapter);
		
		mMargin = (SeekBar)root.findViewById(R.id.margin);
		mMargin.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				settings.setMargin(seekBar.getProgress());
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
			}
		});
		
		mStroke = (SeekBar)root.findViewById(R.id.stroke);
		mStroke.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				settings.setStroke(seekBar.getProgress());
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
			}
		});
		
		mColor = (ImageView)root.findViewById(R.id.color);
		
		mColorR = (SeekBar)root.findViewById(R.id.colorR);
		mColorR.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				settings.setColorR(seekBar.getProgress());
				changeColor();
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
			}
		});
		
		mColorG = (SeekBar)root.findViewById(R.id.colorG);
		mColorG.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				settings.setColorG(seekBar.getProgress());
				changeColor();
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
			}
		});
		
		mColorB = (SeekBar)root.findViewById(R.id.colorB);
		mColorB.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				settings.setColorB(seekBar.getProgress());
				changeColor();
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
			}
		});
		
		mFont = (Spinner)root.findViewById(R.id.font);
		final String[] fonts = getResources().getStringArray(R.array.settings_fonts);
		final ArrayAdapter<String> fontAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, fonts);
		mFont.setAdapter(fontAdapter);
		
		mFontStyle = (Spinner)root.findViewById(R.id.font_style);
		final String[] fontStyles = getResources().getStringArray(R.array.settings_fonts_style);
		final ArrayAdapter<String> fontStyleAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, fontStyles);
		mFontStyle.setAdapter(fontStyleAdapter);
		
		//setupActionBar();
		return root;
	
	}
	
	
	@Override
	public void onPause() {
		super.onPause();

		savePreferences();
	}

	@Override
	public void onResume() {
		super.onResume();
		
		super.onStart();
		
		//bindPreferences();
		
		//changeColor();
		
		getActivity().getWindow().setSoftInputMode(
			      WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
	}
	
	
	private void bindPreferences(){
		
		if( settings.getSignature() != null)
			mSignature.setText(settings.getSignature());
		
		mSize.check(settings.getSize());
		mPosition.setSelection(settings.getPosition());
		mMargin.setProgress(settings.getMargin());
		mStroke.setProgress(settings.getStroke());
		
		mColorR.setProgress(settings.getColorR());
		mColorG.setProgress(settings.getColorG());
		mColorB.setProgress(settings.getColorB());
		
		mFont.setSelection(settings.getTypeFace());
		mFontStyle.setSelection(settings.getTypeFaceStyle());
		
	}
	
	private void savePreferences(){
		settings.setSignature(mSignature.getText().toString());
		settings.setSize(mSize.getCheckedRadioButtonId());
		settings.setPosition(mPosition.getSelectedItemPosition());
		settings.setMargin(mMargin.getProgress());
		settings.setStroke(mStroke.getProgress());
		settings.setTypeFace(mFont.getSelectedItemPosition());
		settings.setTypeFaceStyle(mFontStyle.getSelectedItemPosition());
		
		settings.setColorR(mColorR.getProgress());
		settings.setColorG(mColorG.getProgress());
		settings.setColorB(mStroke.getProgress());
		
		settings.save();
		
	}
	
	private void changeColor(){
		int r = settings.getColorR();
		int g = settings.getColorG();
		int b = settings.getColorB();
		
		Bitmap mBitmap = Bitmap.createBitmap(96, 96,
				Bitmap.Config.ARGB_8888);
		
		Canvas c = new Canvas(mBitmap);
		
		c.drawColor(Color.argb(255, r, g, b));
		
		mColor.setImageBitmap(mBitmap);
		
		
	}

	
}
